package com.yanze.schoolinformation.view.information.book;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.os.Bundle;
import android.widget.ImageView;

import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.base.BaseHeaderActivity;
import com.yanze.schoolinformation.databinding.ActivityBookDetailBinding;
import com.yanze.schoolinformation.databinding.HeaderBookDetailBinding;
import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.book.BookDetailBean;
import com.yanze.schoolinformation.model.bean.book.BooksBean;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;
import com.yanze.schoolinformation.utils.CommonUtils;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.view.webview.WebViewActivity;

import io.reactivex.disposables.Disposable;

public class BookDetailActivity extends BaseHeaderActivity<HeaderBookDetailBinding,ActivityBookDetailBinding> {

    private BooksBean mBooksBean;
    private String mBookDetailUrl;
    private String mBookDetailName;
    public final static String EXTRA_PARAM = "bookBean";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_detail);

        if (getIntent() != null) {
            mBooksBean = (BooksBean) getIntent().getSerializableExtra(EXTRA_PARAM);
        }

        setMotion(setHeaderPicView(), true);
        initSlideShapeTheme(setHeaderImgUrl(), setHeaderImageView());

        setTitle(mBooksBean.getTitle());
        setSubTitle("作者：" + mBooksBean.getAuthor());
        bindingHeaderView.setBooksBean(mBooksBean);
        bindingHeaderView.executePendingBindings();

        loadBookDetail();
    }

    private void loadBookDetail() {
        DataReqo.getInstant().getBookDetail(C.url.API_DOUBAN, mBooksBean.getId())
                .subscribe(new DeforObserver<BookDetailBean>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        addDisposable(disposable);
                    }

                    @Override
                    public void _onNext(BookDetailBean entity) {
                        mBookDetailUrl = entity.getAlt();
                        mBookDetailName = entity.getTitle();
                        bindingContentView.setBookDetailBean(entity);
                        bindingContentView.executePendingBindings();
                    }

                    @Override
                    public void _onError(String msg) {
                        ToastUtils.show(msg);
                        showError();
                    }

                    @Override
                    public void onComplete() {
                        showContentView();
                    }
                });
    }


    @Override
    protected void setTitleClickMore() {
        WebViewActivity.loadUrl(this,mBookDetailUrl,mBookDetailName);
    }

    @Override
    protected int setHeaderLayout() {
        return R.layout.header_book_detail;
    }

    @Override
    protected String setHeaderImgUrl() {
        if (mBooksBean == null) {
            return "";
        }
        return mBooksBean.getImages().getMedium();
    }

    @Override
    protected ImageView setHeaderImageView() {
        return bindingHeaderView.imgItemBg;
    }

    @Override
    protected ImageView setHeaderPicView() {
        return bindingHeaderView.ivOnePhoto;
    }

    @Override
    protected void onRefresh() {
        loadBookDetail();
    }

    /**
     * @param context      activity
     * @param positionData bean
     * @param imageView    imageView
     */
    public static void start(Activity context, BooksBean positionData, ImageView imageView) {
        Intent intent = new Intent(context, BookDetailActivity.class);
        intent.putExtra(EXTRA_PARAM, positionData);
        ActivityOptionsCompat options =
                ActivityOptionsCompat.makeSceneTransitionAnimation(context,
                        imageView, CommonUtils.getString(R.string.transition_book_img));//与xml文件对应
        ActivityCompat.startActivity(context, intent, options.toBundle());
    }
}
